package com.service.impl;

import com.entity.PageBean;
import com.entity.Supplier;
import com.exception.SupplierUsedException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.SupplierMapper;
import com.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;


    @Override
    public PageBean<Supplier> getAll(Integer pageIndex, Integer pageSize, Supplier supplier) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Supplier> list= supplierMapper.selectAll(supplier);
        PageInfo<Supplier> pageInfo = new PageInfo<>(list);
        PageBean<Supplier> pageBean = new PageBean<>(
                pageIndex,//第几页
                pageSize,//每页显示的条数
                pageInfo.getPages(),//总页数
                (int)pageInfo.getTotal(),//数据总条数
                pageInfo.getList()//当前页的数据集合
        );
        return pageBean;
    }

    @Override
    public List<Supplier> findAll() {
        return supplierMapper.findAll();
    }

    @Override
    public void add(Supplier supplier) {
        supplierMapper.insert(supplier);
    }

    @Override
    public boolean getnumPurReqAndSupplierBySid(int sid) {
        return supplierMapper.findnumPurReqAndSupplierBySid(sid) !=null;
    }


    @Override
    public void removeBySid(int sid) {
        if(getnumPurReqAndSupplierBySid(sid)){
            throw new SupplierUsedException("供应商下存在商品，无法删除");
        }
        supplierMapper.deleteBySid(sid);

    }

    @Override
    public void update(Supplier supplier) {
        supplierMapper.update(supplier);
    }
}
